<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8" import="org.apache.commons.fileupload.*,java.io.*,Funcionalidad.*, org.apache.commons.fileupload.servlet.ServletFileUpload, org.apache.commons.fileupload.disk.DiskFileItemFactory, org.apache.commons.io.FilenameUtils, java.util.*, java.io.File, java.lang.Exception"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <h1>Data Received at the Server</h1>
        <hr/>
        <p>
            <%
            String ruta="";
        if (ServletFileUpload.isMultipartContent(request)) {
            ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
            List fileItemsList = servletFileUpload.parseRequest(request);
            String optionalFileName = "";
            FileItem fileItem = null;
            Iterator it = fileItemsList.iterator();
            while (it.hasNext()) {
                FileItem fileItemTemp = (FileItem) it.next();
                if (fileItemTemp.isFormField()) {
                        if (fileItemTemp.getFieldName().equals("filename")) {
                            optionalFileName = fileItemTemp.getString();
                        }
                    } else
                        fileItem = fileItemTemp;
                }
                if (fileItem != null) {
                    String fileName = fileItem.getName();

        /* Save the uploaded file if its size is greater than 0. */
        if (fileItem.getSize() > 0) {
            if (optionalFileName.trim().equals("")) {
                fileName = FilenameUtils.getName(fileName);
            } else {
                fileName = optionalFileName;
            }
            String dirName = application.getRealPath("fotos")+"\\";
            ruta=dirName + fileName;
            File saveTo = new File(ruta);
            String ruta2="../fotos/";
            int id_galeria=5;
            Fotografias.nuevaFoto(id_galeria, "foto" , ruta2+fileName);
            //Los siguientes pasos, debido al enctype="multipart/form-data" del formulario
            //porque no podemos recibir los getParameter
                    /*
        if (request.getParameter("listagaleria").equals("nueva")) {
            if (request.getParameter("otras").equals("")) {
                out.write("No ha introducido ninguna galer&iacute;a");
            } else {
                Galerias.nuevaGaleria(request.getParameter("otras"));
                id_galeria = Galerias.ultimaGaleria();
            }
        }
        if (!request.getParameter("listagaleria").equals("nueva")) {
            id_galeria = Integer.parseInt(request.getParameter("listagaleria"));
        }
        Fotografias.nuevaFoto(id_galeria, request.getParameter("titulo") , ruta);*/
            try {
                fileItem.write(saveTo);
                response.sendRedirect("../Administracion/AdmonGalerias.jsp");
            %>
            <b>Correcto subiendo la foto</b>
            <%
                } catch (Exception e) {
            %>
            <b>Hubo un error al subir la foto</b>
            <%                    }
                }
            }
        }

        

            %>
        </p>
    </body>
</html>